import Vue from 'vue'
import VueRouter from 'vue-router'
import login from '@/views/login/index'
import layout from '@/views/layout/index'
import notfound from '@/views/notfound/notfound'
import { getToken } from '@/utils/token'
import user from '@/views/layout/user/user'
import datalist from '@/views/layout/datalist/datalist'
import enterpriselist from '@/views/layout/enterpriselist/enterpriselist'
import questionlist from '@/views/layout/questionlist/questionlist'
import subjectlist from '@/views/layout/subjectlist/subjectlist'

Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    name: 'login',
    component: login
  },
  {
    path: '/',
    redirect: 'login'
  },
  {
    path: '/layout',
    component: layout,
    redirect:'/layout/user',
    children: [
      {
        path: 'user',
        component: user
      },
      {
        path: 'datalist',
        component: datalist
      },
      {
        path: 'enterpriselist',
        component: enterpriselist
      },
      {
        path: 'questionlist',
        component: questionlist
      },
      {
        path: 'subjectlist',
        component: subjectlist
      }
    ]
  },
  {
    path: '*',
    component: notfound
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  // console.log(to,from,next);
  if (to.path === '/login') {
    next()
  } else {
    const token = getToken()
    if (token) {
      next()
    } else {
      next('/login')
    }
  }
})

export default router
